// pages/askToBuy/askToBuy.js 
const app = getApp();
import httpsUtils from '../../utils/https.js';
import commonUtils from '../../utils/common.js';
import myUtils from '../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    askToBuyList: [],
    askToBuyLenght: 1,
    sqList: [],
    flList: [],
    flTwoList: [],
    sqShowStatus: false,
    flShowStatus: false,
    pxShowStatus: false,
    areaId: "",
    goodType: "",
    goodParentType: "",
    searchType: "2",
    sqName: "全部商圈",
    flName: "全部分类",
    pxName: "发布时间",
    sqUrl: "../../static/image/x_stoc/sanjiao.png",
    flUrl: "../../static/image/x_stoc/sanjiao.png",
    pxUrl: "../../static/image/x_stoc/sanjiao.png",
    current:1,
    hasNextPage:true,

    column:2, //显示列数

    isClass:false //是否显示分类选择
  },
  /**
   * 取消选择
   */
  cancelSelect:function(){
    this.setData({
      isClass: false,
    })
  },
  /**
   * 切换显示行数
   */
  checkCol: function (e) {
    this.setData({
      column: e.currentTarget.dataset.column,
    })
  },
  /**
   * 回首页
   */
  toindex: function() {
    wx.switchTab({
      url: '../index/index',
    })
  },
  /*发布*/
  askToBuyIssue: function (e) {
    //获取用户信息
    commonUtils.getUserInfoByToken(wx.getStorageSync('utoken'), 12,function(){
      wx.navigateTo({
        url: '../publishToNew/publishToNew?inIndex=false',
      })
    });
  },

  /*选择商圈下拉列表*/
  sqSelect: function (e) {
    var that = this;
    that.setData({
      areaId: e.currentTarget.dataset.areaid,
      current: 1,
      hasNextPage: true,
      askToBuyList: [],
    })
    /**刷新当前页面 */
    that.shopWantBuy();
  },

  /*选择分类*/
  flSelect: function (e) {
    var that = this;
    var flId = e.currentTarget.dataset.typeOneId;
    var flList = that.data.flList;
    for (let obj of flList) {
      if(flId == obj.id){
        obj.select = !obj.select;
      }
    }
    that.setData({
      flList: flList
    })
  },

  /*选择二级分类*/
  flTwoTypeSelect: function (e) {
    var that = this;
    var typeId = e.currentTarget.dataset.typeTwoId;
    var flList = that.data.flList;
    for (let obj of flList) {
      for (let fl of obj.sortTypeList) {
        if (typeId == fl.id) {
          fl.select = !fl.select;
        }
      }
    }
    that.setData({
      flList: flList
    })
  },
  /**
   * 打开选择分类
   */
  openSelect: function (e) {
    this.setData({
      isClass: true
    })
  },
  /**
   * 确认选择分类
   */
  sureSelect:function(e){
    this.setData({
      isClass: false,
      current: 1,
      hasNextPage: true,
      askToBuyList: [],
    })
    //查询求购
    this.shopWantBuy();
  },
  /**
   * 全部分类
   */
  allSelect: function(e) {
    var that = this;
    var flList = that.data.flList;
    for (let obj of flList) {
      obj.select = false;
      for (let fl of obj.sortTypeList) {
        fl.select = false;
      }
    }
    this.setData({
      isClass: false,
      current: 1,
      hasNextPage: true,
      askToBuyList: [],
      flList: flList
    })
    //查询求购
    this.shopWantBuy();
  },
  /**
   * 获取商圈列表
   */
  selectAreaList: function (e) {
    var that = this;
    httpsUtils.getData({
      _url: app.globalData._url + '/tradeArea/selectList',
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        for (let obj of json.data) {
          obj.areaName = obj.areaName.replace("商圈", "");
        }
        that.setData({
          sqList: json.data
        })
      }
    })
  },

  /**
   * 获取分类列表
   */
  selectTypeList: function (e) {
    var that = this;
    httpsUtils.getData({
      _url: app.globalData._url + '/sortType/selectTwoList',
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        for (let obj of json.data.list){
          obj.select=false;
          for (let fl of obj.sortTypeList){
            fl.select = false;
          } 
        }
        that.setData({
          flList: json.data.list
        })
      }
    })
  },

  /**
   * 商家求购列表
   */
  shopWantBuy: function (e) {
    wx.showToast({
      title: '加载中...',
      icon: 'loading'
    })
    setTimeout(function () {
      wx.hideToast()
    }, 5000)
    var that = this;

    //获取分类id
    var sortTypeIds = [];
    for (let obj of that.data.flList) {
      for (let fl of obj.sortTypeList) {
        if (fl.select){
          sortTypeIds.push(fl.id)
        }
      }
    } 

    httpsUtils.getData({
      _url: app.globalData._url + '/newGoods/selectList?current=' + that.data.current + '&tradeAreaId=' + that.data.areaId + "&sortTypeId=" + that.data.goodType + "&sort=" + that.data.searchType + "&sortTypeIds=" + myUtils.concatStr(sortTypeIds,","),
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        var askToBuyList = that.data.askToBuyList.concat(json.data.list);
       
        for(let obj of askToBuyList){
          var timeStr = that.timeFormat(obj.gmtDatetime)
          
          if (timeStr.split(" ").length > 1){
            obj.time2 = timeStr + "发布";
            obj.time3 = timeStr.split(" ")[0] + "发布";
            obj.time4 = timeStr.split(" ")[0] + "发布";

          }else{
            obj.time2 = timeStr;
            obj.time3 = timeStr;
            obj.time4 = timeStr;
          }
        }
        that.setData({
          askToBuyList: askToBuyList,
          askToBuyLenght: json.data.total,
          current: json.data.pageNum,
          hasNextPage: json.data.hasNextPage,
        })
      }
      wx.hideToast();
    })
  },
  /**
   * 时间转换
   */
  timeFormat: function (time) {
    var that = this;
    var nowTime = Date.parse(new Date());
    var subTime = (nowTime - Date.parse(new Date(time))) / 1000;
    var timeStr = '';
    if (subTime < 60) {
      timeStr = '刚刚发布';
    } else if (parseInt(subTime / 60) < 60) {
      timeStr = parseInt((subTime / 60)) + '分钟前发布';
    } else if (parseInt(subTime / (60 * 60)) < 24) {
      timeStr = parseInt(subTime / (60 * 60)) + '小时前发布';
    } else if (parseInt(subTime / (60 * 60 * 24)) < 2) {
      timeStr = '昨天' + time.split(" ")[1] +"发布";
    } else if (parseInt(subTime / (60 * 60 * 24)) < 3) {
      timeStr = '前天' + time.split(" ")[1] + "发布";
    } else {
      timeStr = time;
    }
    return timeStr;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
    /* 初始化商圈列表 */
    that.selectAreaList();

    /* 获取分类列表 */
    that.selectTypeList();

    /*商家上新列表*/
    that.shopWantBuy();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.isFlush == true) {
      app.globalData.isFlush = false;
      this.setData({
        isClass: false,
        current: 1,
        hasNextPage: true,
        askToBuyList: [],
      })
      /*商家上新列表*/
      this.shopWantBuy();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      askToBuyList: [],
      askToBuyLenght: 1,
      sqList: [],
      flList: [],
      flTwoList: [],
      sqShowStatus: false,
      flShowStatus: false,
      pxShowStatus: false,
      areaId: "",
      goodType: "",
      goodParentType: "",
      searchType: "2",
      sqName: "全部商圈",
      flName: "全部分类",
      pxName: "发布时间",
      sqUrl: "../../static/image/x_stoc/sanjiao.png",
      flUrl: "../../static/image/x_stoc/sanjiao.png",
      pxUrl: "../../static/image/x_stoc/sanjiao.png",
      current: 1,
      hasNextPage: true,
      isClass: false //是否显示分类选择
    })
    /* 初始化商圈列表 */
    that.selectAreaList();

    /* 获取分类列表 */
    that.selectTypeList();

    /*商家上新列表*/
    that.shopWantBuy();

    wx.stopPullDownRefresh(); 
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.hasNextPage){
      that.setData({
        current: that.data.current + 1,
      })
      /*商家求购列表*/
      that.shopWantBuy();
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '服装上新，世界同步',
      path: 'pages/askToNew/askToNew',
    }
  },
})